package com.bright.hxj.cwduijie.service.impl;

import com.bright.hxj.cwduijie.enums.DataApi;
import com.bright.hxj.cwduijie.pojo.bo.ApiDataCollection;
import com.bright.hxj.cwduijie.pojo.bo.FixedAssetsSub;
import com.bright.hxj.cwduijie.pojo.po.FixedAssets;
import com.bright.hxj.cwduijie.pojo.query.DuijieQuery;
import com.bright.hxj.cwduijie.repository.FixedAssetsRepository;
import com.bright.hxj.cwduijie.service.AbstractPerThousandApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;


@Component
@RequiredArgsConstructor
@Slf4j
public class ApiFixedAssetsServiceImpl extends AbstractPerThousandApiService<FixedAssets> {

    private final FixedAssetsRepository fixedAssetsRepository;

    @Override
    public JpaSpecificationExecutor getJpaSpecificationExecutor() {
        return fixedAssetsRepository;
    }

    @Override
    public DataApi getDataApi() {
        return DataApi.FIXED_ASSETS;
    }

    @Override
    public Function<FixedAssets, Integer> getIdFunc() {
        return FixedAssets::getId;
    }

    @Override
    public Function<FixedAssets, String> getDataKeyFunc() {
        return FixedAssets::getDATA_KEY;
    }

    @Override
    public Function<ApiDataCollection, List<FixedAssets>> getDataFunc() {
        return ApiDataCollection::getFixedAssets;
    }

    @Override
    public DataApi getDeleteDataApi() {
        return DataApi.DELETE_FIXED_ASSETS;
    }


    @Override
    protected void getData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery) {
//        List<FixedAssets> fixedAssets = fixedAssetsRepository.getAllByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.WAITING.getValue(), OpType.ADD_OR_EDIT.getValue());
//        fixedAssets.addAll(fixedAssetsRepository.getAllByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.ERROR.getValue(), OpType.ADD_OR_EDIT.getValue()));
        List<FixedAssets> fixedAssets = getNeedDuijieData(duijieQuery);
        // v1.6.17 增加赋值资产明细列表
        for (FixedAssets fixedAsset : fixedAssets) {
            if (fixedAsset.getBUILDING_AREA() != null && fixedAsset.getSTATE() != null) {
                FixedAssetsSub fixedAssetsSub = new FixedAssetsSub();
                fixedAssetsSub.setNAME(fixedAsset.getMC());
                fixedAssetsSub.setBUILDING_AREA(fixedAsset.getBUILDING_AREA());
                fixedAssetsSub.setSTATE(fixedAsset.getSTATE());
                fixedAsset.setSUB_ASSET_LIST(Collections.singletonList(fixedAssetsSub));
            }
        }
        apiDataCollection.setFixedAssets(fixedAssets);
    }

//    @Override
//    protected List<FixedAssets> getNeedDeleteData(DuijieQuery duijieQuery) {
//        List<FixedAssets> fixedAssets = fixedAssetsRepository.getAllByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.WAITING.getValue(), OpType.DEL.getValue());
//        fixedAssets.addAll(fixedAssetsRepository.getAllByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.ERROR.getValue(), OpType.DEL.getValue()));
//        return fixedAssets;
//    }

    public List<Integer> getIdsByDataKeys(List<String> dataKeys) {
        return fixedAssetsRepository.getIdListByDataKeys(dataKeys);
    }

}
